import axios from "axios";
import {ElMessage} from "element-plus";
import useUserStore from "@/store/modules/user/user";
import {cookie} from "@/utils/cookie";
//创建axios实例
let request = axios.create({
    // baseURL: import.meta.env.VITE_APP_BASE_API,
    baseURL: '',
})
//请求拦截器
request.interceptors.request.use(config => {
    const userStore = useUserStore()
    if (userStore.token) {
        config.headers.rrtoken = userStore.token.value
    }
    return config;
});
//响应拦截器
type Data<T> = {
    code: number,
    msg: string,
    data: T
}

request.interceptors.response.use((response) => {
    let code = response.data.code;
    let msg = ''
    if (code === 0) {
        return response.data;
    } else {
        if (code === 400) {
            cookie.removeCookie('token')
        } else {
            //todo
        }
        ElMessage({
            type: 'error',
            message: response.data.code + ',' + response.data.msg
        })
        return Promise.reject(response.data);
    }
}, (error) => {
    //处理网络错误
    let msg = '';
    console.log(error)
    ElMessage({
        type: 'error',
        message: error.code + ',' + error.message
    })
    return Promise.reject(error);
});
export default request;
